home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ww_tv.exe / TVIEW.CPP < prev    next >
C/C++ Source or Header  |  1992-07-05  |  20KB  |  804 lines

  1. #pragma warn -stv
  2. /*------------------------------------------------------------*/
  3. /* filename -       tview.cpp                                 */
  4. /*                                                            */
  5. /* function(s)                                                */
  6. /*                  TView member functions                    */
  7. /*------------------------------------------------------------*/
  8.  
  9. /*------------------------------------------------------------*/
  10. /*                                                            */
  11. /*    Turbo Vision -  Version 1.0                             */
  12. /*                                                            */
  13. /*                                                            */
  14. /*    Copyright (c) 1991 by Borland International             */
  15. /*    All Rights Reserved.                                    */
  16. /*                                                            */
  17. /*------------------------------------------------------------*/
  18.  
  19. //  Primatech Modification History:
  20. //
  21. //      11-25-91 JLS  Changed change() to have the shift state passed in
  22. //      12-19-91 JLS  Added STACKCHECK() (only effects #ifdef DEBUG)
  23. //      05-27-92 JLS  In setState(), only tell owner to reset current view 
  24. //                      when it makes sense to do it.
  25. //      05-28-92 JLS  In select(), if ofTopSelect and already first, call
  26. //                      owner->setCurrent() instead of makeFirst()
  27. //      07-05-92 JLS  Sync with TV1.03 (BC3.10) -- cosmetic changes
  28. //
  29.  
  30. #define Uses_TKeys
  31. #define Uses_TView
  32. #define Uses_TCommandSet
  33. #define Uses_TPoint
  34. #define Uses_TGroup
  35. #define Uses_TRect
  36. #define Uses_TEvent
  37. #define Uses_opstream
  38. #define Uses_ipstream
  39. #include <tv.h>
  40.  
  41. #if !defined( __DOS_H )
  42. #include <Dos.h>
  43. #endif  // __DOS_H
  44.  
  45. #if !defined( __LIMITS_H )
  46. #include <Limits.h>
  47. #endif  // __LIMITS_H
  48.  
  49. #include "StackChk.h"
  50.  
  51. TPoint shadowSize = {2,1};
  52. uchar shadowAttr = 0x08;
  53. Boolean near TView::showMarkers = False;
  54. uchar near TView::errorAttr = 0xCF;
  55. Boolean near TView::commandSetChanged = False;
  56.  
  57. extern TView *TheTopView;
  58.  
  59. static TCommandSet initCommands()
  60. {
  61.     TCommandSet temp;
  62.     for( int i = 0; i < 256; i++ )
  63.         temp.enableCmd( i );
  64.     temp.disableCmd( cmZoom );
  65.     temp.disableCmd( cmClose );
  66.     temp.disableCmd( cmResize );
  67.     temp.disableCmd( cmNext );
  68.     temp.disableCmd( cmPrev );
  69.     return temp;
  70. }
  71.  
  72. TCommandSet near TView::curCommandSet = initCommands();
  73.  
  74. TView::TView( const TRect& bounds) :
  75.     owner( 0 ), next( 0 ), options( 0 ), state( sfVisible ),
  76.     growMode( 0 ), dragMode( dmLimitLoY ), helpCtx( hcNoContext ),
  77.     eventMask( evMouseDown | evKeyDown | evCommand )
  78. {
  79.     setBounds( bounds);
  80.     cursor.x = cursor.y = 0;
  81. }
  82.  
  83. TView::~TView()
  84. {
  85. }
  86.  
  87. void TView::blockCursor()
  88. {
  89.      setState(sfCursorIns, True);
  90. }
  91.  
  92. #define grow(i) (( (growMode & gfGrowRel)) ? \
  93.                 (i = (i * s + ((s - d) >> 1)) / (s - d)) : (i += d))
  94.  
  95. inline int range( int val, int min, int max )
  96. {
  97.     if( val < min )
  98.         return min;
  99.     else if( val > max )
  100.         return max;
  101.     else
  102.         return val;
  103. }
  104.                 
  105. void TView::calcBounds( TRect& bounds, TPoint delta )
  106. {
  107.     bounds = getBounds();
  108.  
  109.     short s = owner->size.x;
  110.     short d = delta.x;
  111.  
  112.     if( (growMode & gfGrowLoX) != 0 )
  113.         grow(bounds.a.x);
  114.  
  115.     if( (growMode & gfGrowHiX) != 0 )
  116.         grow(bounds.b.x);
  117.  
  118.     s = owner->size.y;
  119.     d = delta.y;
  120.  
  121.     if( (growMode & gfGrowLoY) != 0 )
  122.         grow(bounds.a.y);
  123.  
  124.     if( (growMode & gfGrowHiY) != 0 )
  125.         grow(bounds.b.y);
  126.  
  127.     TPoint minLim, maxLim;
  128.     sizeLimits( minLim, maxLim );
  129.     bounds.b.x = bounds.a.x + range( bounds.b.x-bounds.a.x, minLim.x, maxLim.x );
  130.     bounds.b.y = bounds.a.y + range( bounds.b.y-bounds.a.y, minLim.y, maxLim.y );
  131. }
  132.  
  133. void TView::changeBounds( const TRect& bounds )
  134. {
  135.     setBounds(bounds);
  136.     drawView();
  137. }
  138.  
  139. void TView::clearEvent( TEvent& event )
  140. {
  141.     STACKCHECK();
  142.  
  143.     event.what = evNothing;
  144.     event.message.infoPtr = this;
  145. }
  146.  
  147. Boolean TView::commandEnabled( ushort command )
  148. {
  149.     return Boolean((command > 255) || curCommandSet.has(command));
  150. }
  151.  
  152. ushort TView::dataSize()
  153. {
  154.     return 0;
  155. }
  156.  
  157. void TView::disableCommands( TCommandSet& commands )
  158. {
  159.     commandSetChanged = Boolean( commandSetChanged ||
  160.                                 !(curCommandSet & commands).isEmpty());
  161.     curCommandSet.disableCmd(commands);
  162. }
  163.  
  164. void TView::disableCommand( ushort command )
  165. {
  166.     commandSetChanged = Boolean( commandSetChanged ||
  167.                                  curCommandSet.has(command) );
  168.     curCommandSet.disableCmd(command);
  169. }
  170.  
  171. void TView::moveGrow( TPoint p,
  172.                       TPoint s,
  173.                       TRect& limits,
  174.                       TPoint minSize,
  175.                       TPoint maxSize,
  176.                       uchar mode
  177.                     )
  178. {
  179.     TRect   r;
  180.     s.x = min(max(s.x, minSize.x), maxSize.x);
  181.     s.y = min(max(s.y, minSize.y), maxSize.y);
  182.     p.x = min(max(p.x, limits.a.x - s.x+1), limits.b.x-1);
  183.     p.y = min(max(p.y, limits.a.y - s.y+1), limits.b.y-1);
  184.  
  185.     if( (mode & dmLimitLoX) != 0 )
  186.         p.x = max(p.x, limits.a.x);
  187.     if( (mode & dmLimitLoY) != 0 )
  188.         p.y = max(p.y, limits.a.y);
  189.     if( (mode & dmLimitHiX) != 0 )
  190.         p.x = min(p.x, limits.b.x-s.x);
  191.     if( (mode & dmLimitHiY) != 0 )
  192.         p.y = min(p.y, limits.b.y-s.y);
  193.     r = TRect(p.x, p.y, p.x +  s.x, p.y +  s.y);
  194.     locate(r);
  195. }
  196.  
  197. void TView::change( uchar mode, TPoint delta, TPoint& p, TPoint& s, uchar shiftState )
  198. {
  199.     if( (mode & dmDragMove) != 0 && (shiftState & 3) == 0 )
  200.         p += delta;
  201.     else if( (mode & dmDragGrow) != 0 && (shiftState & 3) != 0 )
  202.         s += delta;
  203. }
  204.  
  205. void TView::dragView( TEvent& event,
  206.                       uchar mode,
  207.                       TRect& limits,
  208.                       TPoint minSize,
  209.                       TPoint maxSize
  210.                     )
  211. {
  212.     TRect saveBounds;
  213.  
  214.     TPoint p, s;
  215.     setState( sfDragging, True );
  216.  
  217.     if( event.what == evMouseDown )
  218.         {
  219.         if( (mode & dmDragMove) != 0 )
  220.             {
  221.             p = origin - event.mouse.where;
  222.             do  {
  223.                 event.mouse.where += p;
  224.                 moveGrow( event.mouse.where,
  225.                           size,
  226.                           limits,
  227.                           minSize,
  228.                           maxSize,
  229.                           mode
  230.                         );
  231.                 } while( mouseEvent(event,evMouseMove) );
  232.             }
  233.         else
  234.             {
  235.             p = size - event.mouse.where;
  236.             do  {
  237.                 event.mouse.where += p;
  238.                 moveGrow( origin,
  239.                           event.mouse.where,
  240.                           limits,
  241.                           minSize,
  242.                           maxSize,
  243.                           mode
  244.                         );
  245.                 } while( mouseEvent(event,evMouseMove) );
  246.             }
  247.         }
  248.     else
  249.         {
  250.         static TPoint 
  251.             goLeft      =   {-1, 0}, 
  252.             goRight     =   { 1, 0}, 
  253.             goUp        =   { 0,-1}, 
  254.             goDown      =   { 0, 1}, 
  255.             goCtrlLeft  =   {-8, 0}, 
  256.             goCtrlRight =   { 8, 0};
  257.             
  258.         saveBounds = getBounds();
  259.         do  {
  260.             p = origin;
  261.             s = size;
  262.             keyEvent(event);
  263.             switch (event.keyDown.keyCode & 0xFF00)
  264.                 {
  265.                 case kbLeft:
  266.                     change(mode, goLeft, p, s, event.shiftState);
  267.                     break;
  268.                 case kbRight:
  269.                     change(mode, goRight, p, s, event.shiftState);
  270.                     break;
  271.                 case kbUp:
  272.                     change(mode, goUp, p, s, event.shiftState);
  273.                     break;
  274.                 case kbDown:
  275.                     change(mode, goDown, p, s, event.shiftState);
  276.                     break;
  277.                 case kbCtrlLeft:
  278.                     chang